# core/sync/metadata_hash.py
import xxhash

class MetadataHasher:
    def __init__(self):
        self.hasher = xxhash.xxh64()
        
    def compute_hash(self, metadata_dict):
        # 按字段排序保证一致性
        sorted_items = sorted(metadata_dict.items())
        for key, value in sorted_items:
            self.hasher.update(f"{key}:{value}".encode('utf-8'))
        return self.hasher.digest()

class SmartSync:
    def sync(self, local_db, usb_db):
        # 使用元数据哈希对比
        local_hash = self.hasher.compute(local_metadata)
        usb_hash = self.hasher.compute(usb_metadata)
        
        if local_hash != usb_hash:
            self._resolve_conflict(local_db, usb_db)